package com.bcbim.smart_water.health.service;

import com.bcbim.common.sys.Code;
import com.bcbim.smart_water.health.mapper.DefectMapper;
import com.bcbim.smart_water.health.mapper.HealthMapper;
import com.bcbim.smart_water_interface.health.bo.HealthBO;
import com.bcbim.smart_water_interface.health.bo.UpdateFlawLatLonBO;
import com.bcbim.smart_water_interface.health.dao.FlawDO;
import com.bcbim.smart_water_interface.health.dto.*;
import com.bcbim.smart_water_interface.health.service.HealthService;
import com.bcbim.smart_water_interface.health.vo.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 健康监测接口
 * @Author: ye
 * @Date: 2021/8/27
 * @Version: V1.0
 **/
@Service(timeout = 60 * 1000, group = "health", version = "1.0.0")
public class HealthServiceImpl implements HealthService {

    @Autowired
    private HealthMapper healthMapper;
    @Autowired
    private DefectMapper defectMapper;

    /**
     * @Description: 健康监测总数
     * @Param: ListHealthDto
     * @Return: int
     * @Author: ye
     * @Date: 2021/8/27
     */
    @Override
    public int countHealth(ListHealthDTO query) {
        return healthMapper.countHealth(query);
    }

    /**
     * @Description: 健康检测列表
     * @Param: ListHealthDto
     * @Return: List<HealthVO>
     * @Author: ye
     * @Date: 2021/8/27
     */
    @Override
    public List<HealthVO> listHealth(ListHealthDTO query) {
        return healthMapper.listHealth(query);
    }

    /**
     * @Description: 缺陷种类查询
     * @Param: defectType 缺陷类型
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2021/8/27
     * @return
     */
    @Override
    public List<Code> getDefect(String defectType) {
        return defectMapper.getDefect(defectType);
    }

    /**
     * @Description: 健康检测信息
     * @Param: healthId
     * @Return: HealthInfoVo
     * @Author: ye
     * @Date: 2021/8/30
     */
    @Override
    public HealthInfoVO getHealthInfoByHealthId(String healthId) {
        return healthMapper.getHealthInfoByHealthId(healthId);
    }

    /**
     * @Description: 缺陷信息
     * @Param: healthId
     * @Return: List<FlawInfoVo>
     * @Author: ye
     * @Date: 2021/8/30
     */
    @Override
    public List<FlawInfoVO> getFlawInfoByHealthId(String healthId) {
        return healthMapper.getFlawInfoByHealthId(healthId);
    }

    /**
     * @Description: 功能性缺陷信息
     * @Param: healthId
     * @Return: DefectLevelVO
     * @Author: ye
     * @Date: 2021/8/30
     */
    @Override
    public DefectLevelVO getHealthDefect(String healthId, String defectType) {
        return healthMapper.getHealthDefect(healthId, defectType);
    }

    /**
     * @Description: 更新缺陷位置所需信息
     * @Param:
     * @Return: List<UpdateFlawLatLonBO>
     * @Author: ye
     * @Date: 2021/9/2
     */
    @Override
    public List<UpdateFlawLatLonBO> getUpdateFlawLatLon() {
        return healthMapper.getUpdateFlawLatLon();
    }

    /**
     * @Description: 更新位置
     * @Param:
     * @Return: List<UpdateFlawLatLonDTO>
     * @Author: ye
     * @Date: 2021/9/2
     */
    @Override
    public void updateFlawLocation(List<UpdateFlawLatLonDTO> list) {
        healthMapper.updateFlawLocation(list);
    }

    /**
     * @Description: 查询地图上缺陷位置信息
     * @Param: FlawLocationDTO
     * @Return: List<FlawLocationVO>
     * @Author: ye
     * @Date: 2021/9/2
     */
    @Override
    public List<FlawLocationVO> listFlawLocation(FlawLocationDTO query) {
        List<FlawLocationVO> result = new ArrayList<>();
        boolean structural = false;
        boolean functional = false;
        boolean structuralDefectId = false;
        boolean functionalDefectId = false;

        if (ObjectUtils.isNotEmpty(query.getStructuralDefectLevels()) && query.getStructuralDefectLevels().length > 0) {
            structural = true;
        }
        if (ObjectUtils.isNotEmpty(query.getFunctionalDefectLevels()) && query.getFunctionalDefectLevels().length > 0) {
            functional = true;
        }
        if (ObjectUtils.isNotEmpty(query.getStructuralDefectId()) && query.getStructuralDefectId().length > 0) {
            structuralDefectId = true;
        }
        if (ObjectUtils.isNotEmpty(query.getFunctionalDefectId()) && query.getFunctionalDefectId().length > 0) {
            functionalDefectId = true;
        }

        if (structural || structuralDefectId) {
            result.addAll(healthMapper.listStructuralFlawLocation(query));
        }
        if (functional || functionalDefectId) {
            result.addAll(healthMapper.listFunctionalFlawLocation(query));
        }

        return result;
    }

    /**
     * @Description: 管线健康监测信息
     * @Param: line
     * @Return: HealthInfoVO
     * @Author: ye
     * @Date: 2021/9/16
     */
    @Override
    public HealthInfoVO getHealthByLineId(String lineId) {
        return healthMapper.getHealthByLineId(lineId);
    }

        /**
         * @Description: 管线缺陷信息
         * @Param: lineId
         * @Return: List<FlawLocationVO>
         * @Author: ye
         * @Date: 2021/9/16
         */
    @Override
    public List<FlawLocationVO> getFlawLocationByLineId(String lineId) {
        return healthMapper.getFlawLocationByLineId(lineId);
    }

    /**
     * @Description: 查询健康检测id
     * @Param: ListHealthDTO
     * @Return: List<String>
     * @Author: ye
     * @Date: 2021/10/29
     */
    @Override
    public List<String> getHealthId(ListHealthDTO query) {
        return healthMapper.getHealthId(query);
    }

    /**
     * @param query
     * @Description: 下载集总数
     * @Param: ListDownloadSetHealthDTO
     * @Return: int
     * @Author: ye
     * @Date: 2021/10/29
     */
    @Override
    public int countHealthDownloadSet(ListDownloadSetHealthDTO query) {
        return healthMapper.countHealthDownloadSet(query);
    }

    /**
     * @param query
     * @Description: 下载集列表
     * @Param: ListDownloadSetHealthDTO
     * @Return: List<HealthVO>
     * @Author: ye
     * @Date: 2021/10/29
     */
    @Override
    public List<HealthVO> listHealthDownloadSet(ListDownloadSetHealthDTO query) {
        return healthMapper.listHealthDownloadSet(query);
    }

    /**
     * @Description: 健康检测管理列表
     * @Param: ListHealthManagementDTO
     * @Return: int
     * @Author: ye
     * @Date: 2021/11/1
     */
    @Override
    public int countHealthManagement(ListHealthManagementDTO query) {
        return healthMapper.countHealthManagement(query);
    }

    /**
     * @Description: 健康检测管理列表
     * @Param: ListHealthManagementDTO
     * @Return: List<HealthManagementVO>
     * @Author: ye
     * @Date: 2021/11/1
     */
    @Override
    public List<HealthManagementVO> listHealthManagement(ListHealthManagementDTO query) {
        return healthMapper.listHealthManagement(query);
    }

    /**
     * @Description: 删除健康检测
     * @Param: healthId
     * @Return: int
     * @Author: ye
     * @Date: 2021/11/1
     */
    @Override
    public int removeHealth(String healthId) {
        return healthMapper.removeHealth(healthId);
    }

    /**
     * @Description: 上传管壁全景图
     * @Param: UploadHealthImgDTO
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2021/11/1
     */
    @Override
    public void uploadLineWallImg(UploadHealthImgDTO query) {
        healthMapper.uploadLineWallImg(query);
    }

    /**
     * @Description: 上传模型截面图
     * @Param: UploadHealthImgDTO
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2021/11/1
     */
    @Override
    public void uploadLineSectionImg(UploadHealthImgDTO query) {
        healthMapper.uploadLineSectionImg(query);
    }

    /**
     * @Description: 监控监测管理详情
     * @Param: String
     * @Return: HealthManagementInfoVO
     * @Author: ye
     * @Date: 2021/11/3
     */
    @Override
    public HealthManagementInfoVO getHealthManagementInfo(String healthId) {
        return healthMapper.getHealthManagementInfo(healthId);
    }

    /**
     * @Description: 健康监测缺陷管理详情
     * @Param: String
     * @Return: List<FlawManagementInfoVO>
     * @Author: ye
     * @Date: 2021/11/3
     */
    @Override
    public List<FlawManagementInfoVO> getFlawManagementInfo(String healthId) {
        return healthMapper.getFlawManagementInfo(healthId);
    }

    /**
     * @param healthManagementInfoVO
     * @Description: 编辑保存健康监测
     * @Param: HealthManagementInfoVO
     * @Return:
     * @Author: ye
     * @Date: 2021/11/3
     */
    @Override
    public void updateHealthInfo(HealthManagementInfoVO healthManagementInfoVO) {
        healthMapper.updateHealthInfo(healthManagementInfoVO);
    }

    /**
     * @param arrFlaw
     * @Description: 编辑保存健康监测缺陷
     * @Param: List<FlawDO>
     * @Return:
     * @Author: ye
     * @Date: 2021/11/3
     */
    @Override
    public void insertFlawInfo(List<FlawDO> arrFlaw) {
        healthMapper.insertFlawInfo(arrFlaw);
    }

    /**
     * @Description: 清除健康监测缺陷
     * @Param: healthId
     * @Return:
     * @Author: ye
     * @Date: 2021/11/3
     */
    @Override
    public void clearHealthFlaw(String healthId) {
        healthMapper.clearHealthFlaw(healthId);
    }

    /**
     * @Description: 查询健康检测管线经纬度位置
     * @Param: healthId
     * @Return: UpdateFlawLatLonBO
     * @Author: ye
     * @Date: 2021/11/3
     */
    @Override
    public UpdateFlawLatLonBO getHealthLineLatLon(String healthId) {
        UpdateFlawLatLonBO healthLineLatLon = healthMapper.getHealthLineLatLon(healthId);
        if (ObjectUtils.isEmpty(healthLineLatLon) ||
                ObjectUtils.isEmpty(healthLineLatLon.getStartLat()) ||
                ObjectUtils.isEmpty(healthLineLatLon.getStartLon()) ||
                ObjectUtils.isEmpty(healthLineLatLon.getEndLat()) ||
                ObjectUtils.isEmpty(healthLineLatLon.getEndLon())) {
            return null;
        }
        return healthLineLatLon;
    }

    /**
     * @Description: 查询缺陷代码
     * @Param:
     * @Return: List<SelectDefectVO>
     * @Author: ye
     * @Date: 2021/11/4
     */
    @Override
    public List<SelectDefectVO> selectDefect() {
        return defectMapper.selectDefect();
    }

    /**
     * @Description: 缺陷等级
     * @Param: defectId
     * @Return: List<SelectDefectLevelVO>
     * @Author: ye
     * @Date: 2021/11/4
     */
    @Override
    public List<SelectDefectLevelVO> selectDefectLevel(String defectId) {
        return defectMapper.selectDefectLevel(defectId);
    }

    /**
     * @param lineId
     * @Description: 清除管线健康检测
     * @Param: lineId
     * @Return:
     * @Author: ye
     * @Date: 2021/11/13
     */
    @Override
    public void removeLineHealth(String lineId) {
        healthMapper.removeLineHealth(lineId);
    }

    /**
     * @Description: 新建健康监测
     * @Param: HealthBO
     * @Return:
     * @Author: ye
     * @Date: 2021/11/13
     */
    @Override
    public void insertHealth(HealthBO health) {
        healthMapper.insertHealth(health);
    }

    /**
     * @Description: 保存健康监测视频
     * @Param: UploadHealthVideoDTO
     * @Return:
     * @Author: ye
     * @Date: 2021/11/13
     */
    @Override
    public void saveHealthVideo(UploadHealthVideoDTO videoDTO) {
        healthMapper.saveHealthVideo(videoDTO);
    }

    /**
     * @param substring
     * @Description: 查询缺陷id
     * @Param:
     * @Return:
     * @Author: ye
     * @Date: 2021/11/13
     */
    @Override
    public String getDefectId(String substring) {
        return healthMapper.getDefectId(substring);
    }

    /**
     * @param defectId
     * @param s
     * @Description: 查询缺陷等级
     * @Param:
     * @Return:
     * @Author: ye
     * @Date: 2021/11/13
     */
    @Override
    public String getDefectLevelId(String defectId, String s) {
        return healthMapper.getDefectLevelId(defectId, s);
    }

}
